package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.AppImCustomerService;
import com.ruoyi.app.domain.bo.AppImCustomerServiceBo;
import com.ruoyi.app.domain.vo.AppImCustomerServiceVo;
import com.ruoyi.app.mapper.AppImCustomerServiceMapper;
import com.ruoyi.app.service.IAppImCustomerServiceService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客服Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RequiredArgsConstructor
@Service
public class AppImCustomerServiceServiceImpl implements IAppImCustomerServiceService {

    private final AppImCustomerServiceMapper baseMapper;

    /**
     * 查询客服
     */
    @Override
    public AppImCustomerServiceVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客服列表
     */
    @Override
    public TableDataInfo<AppImCustomerServiceVo> queryPageList(AppImCustomerServiceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppImCustomerService> lqw = buildQueryWrapper(bo);
        Page<AppImCustomerServiceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客服列表
     */
    @Override
    public List<AppImCustomerServiceVo> queryList(AppImCustomerServiceBo bo) {
        LambdaQueryWrapper<AppImCustomerService> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppImCustomerService> buildQueryWrapper(AppImCustomerServiceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppImCustomerService> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), AppImCustomerService::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), AppImCustomerService::getAvatar, bo.getAvatar());
        lqw.eq(bo.getStatus() != null, AppImCustomerService::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增客服
     */
    @Override
    public Boolean insertByBo(AppImCustomerServiceBo bo) {
        AppImCustomerService add = BeanUtil.toBean(bo, AppImCustomerService.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客服
     */
    @Override
    public Boolean updateByBo(AppImCustomerServiceBo bo) {
        AppImCustomerService update = BeanUtil.toBean(bo, AppImCustomerService.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppImCustomerService entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客服
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
